<template >
  <div style="margin-right: 30px">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-col :span="12">
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input> </el-form-item
      ></el-col>
      <el-col :span="12">
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            show-password
          ></el-input> </el-form-item
      ></el-col>
      <el-col :span="12">
        <el-form-item label="姓名" prop="createName">
          <el-input v-model="ruleForm.createName"></el-input> </el-form-item
      ></el-col>
      <el-col :span="12">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input> </el-form-item
      ></el-col>

      <el-col :span="23">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="https://localhost:5001/api/UploadFile/Upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="ruleForm.avatar"
              :src="'https://localhost:5001/file/' + ruleForm.avatar"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-form>
  </div>
</template>
<script>
import { AddUser } from "@/api/admin";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        createName: "",
        email: "",
        remark: "无",
        avatar: "",
      },
      imageUrl: "",
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.avatar = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    Adduser(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          AddUser(this.ruleForm).then((res) => {
            if (res.data) {
              this.$message.error("添加成功");
            }else{
              this.$message.error("添加失败");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 88px;
  height: 88px;
  line-height: 88px;
  text-align: center;
}
.avatar {
  width: 88px;
  height: 88px;
  display: block;
}
</style>